
package multiNyID.MarkerModel;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Switch;
import javax.vecmath.Point2i;

/**
 * class GridModel
 *
 * model untuk menghandle objek di papan catur
 * 
 * @author whe_green@yahoo.co.id
 * Analisis Metode Occlusion Based Pada Augmented Reality
 * Studi Kasus Interaksi dengan Objek Virtual Secara Realtime
 * Menggunakan Gerakan Tangan
 *
 * Institut Teknologi Telkom
 * Fakultas Informatika 2007
 * Bandung Indonesia
 *
 * nyargreen.wordpress.com
 */

public class GridModel{

  private final int UKURAN_PAPAN = 8;

  private ManipulasiModel[][] posModel;
//  private ManipulasiModel cadangan;
  private BranchGroup gridBG;
//  private BranchGroup BG;
  private Point2i posAwal;
  private Point2i posAkhir;
  private boolean bawah=false;
  private Switch visSwitch;
  
  public GridModel(){

    
      posModel = new ManipulasiModel[UKURAN_PAPAN][UKURAN_PAPAN];
      gridBG = new BranchGroup();
      gridBG.setCapability(BranchGroup.ALLOW_DETACH);
      gridBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
      gridBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

      posAwal=new Point2i();
      posAkhir=new Point2i();
          // create switch for model visibility
    visSwitch = new Switch();
    visSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
    visSwitch.addChild( gridBG );
    visSwitch.setWhichChild( Switch.CHILD_ALL );   // make invisible
    
//    BG.addChild(visSwitch);
  }

  public ManipulasiModel getModel(int x,int y){
//      if ((x>UKURAN_PAPAN)||(y>UKURAN_PAPAN)||(x<0)||(y<0)){
//          return null;
//      }
      return posModel[x][y];
  }

  public void addModel(ManipulasiModel model, int x, int y){
      if (cekPosisi(x, y)){
          
          movePosisi(model);
          posModel[x][y] = model;          
          gridBG.addChild(getModel(x, y).getModelBG());
          setPosAwal(x, y);
//          System.out.println("masuk ke array = "+x+"- "+y);
      }      
  }
//  public void addCadangan(){
//
//  }

  public void removeModel(int x, int y){
      gridBG.removeChild(getModel(x, y).getModelBG());
      posModel[x][y] = null;
      
  }

  public boolean cekPosisi(int x, int y){
      //true kalo masih kosong
      if (getModel(x, y) == null){
          return true;
      }
      else{
          return false;
      }
  }

    public void setPosAkhir(int x, int y) {
        this.posAkhir.setX(x);
        this.posAkhir.setY(y);
    }

    public void setPosAwal(int x, int y) {
        this.posAwal.setX(x);
        this.posAwal.setY(y);
    }
  
  public void moveModel(){
      int x = this.posAwal.getX();
      int y = this.posAwal.getY();
      
      System.out.println("hasil konv = "+x+" - "+y);
      if (cekPosisi(x, y)){
          System.out.println("=====model ga masuk array====="+x+" - "+y);
          return;
      }
      else{
          int x2 = posAkhir.getX();
          int y2 = posAkhir.getY();          
          if (cekPosisi(x2, y2)){
              //pindahin
              System.out.println("=====MOVED=====");
              ManipulasiModel temp = getModel(x, y);
//              movePosisi(temp);
              removeModel(x, y);
              addModel(temp, x2, y2);              
          }
          else{
              System.out.println("=====pos akhir isi=====");
              return;
          }
      }
  }

  public void createModel(BranchGroup bg, double skala ,int x,int y,int lihat){
      ManipulasiModel tempModel;
      tempModel= new ManipulasiModel(bg, skala);
      tempModel.setPosObjekGrid();
      setPosAwal(0, 0);
      setPosAkhir(x, y);
      tempModel.switchOn(lihat);
      addModel(tempModel, x, y);
  }
  public void createModel(String modelFnm, double skala, boolean hasOrd, int x,int y ,int lihat){
      ManipulasiModel tempModel;
      tempModel= new ManipulasiModel(modelFnm, skala, hasOrd);
      System.out.println("propman = "+tempModel.getModelPropMan());
      tempModel.setPosObjekGrid();
      setPosAwal(0, 0);
      setPosAkhir(x, y);
      tempModel.switchOn(lihat);      
      addModel(tempModel, x, y);
  }

    public Switch getVisSwitch() {
        return visSwitch;
    }


    public void setSwitchOn(int i){
        visSwitch.setWhichChild(i);
    }



    public void movePosisi(ManipulasiModel model){
        int x = posAkhir.getX() - posAwal.getX();
        int y = posAkhir.getY() - posAwal.getY();
        System.out.println("pindah ke ="+x+" , "+y);
        int arah = (x < 0) ? 1 : 0;
//        System.out.println(model);
        model.getModelPropMan().move(0, arah, Math.abs(x));
        arah = (y < 0) ? 0 : 1;
        model.getModelPropMan().move(2, arah, Math.abs(y));
    }

    public Point2i getPosAwal() {
        return posAwal;
    }

    public Point2i getPosAkhir() {
        return posAkhir;
    }
    

    public void sisiBawah(int x, int y,boolean tampilBawah){
        if((!bawah)&&tampilBawah)
        {
            getModel(x, y).getModelPropMan().rotate(0, 0, 18);
            bawah=true;
        }else if((!tampilBawah)&&(bawah)){
            getModel(x, y).getModelPropMan().rotate(0, 0, 18);
            bawah=false;
        }
    }



    
}
